/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.ai2.manager;

import com.aionengine.gameserver.model.EmotionType;
import com.aionengine.gameserver.model.gameobjects.Creature;
import com.aionengine.gameserver.model.gameobjects.Npc;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.model.gameobjects.state.CreatureState;
import com.aionengine.gameserver.network.aion.serverpackets.SM_EMOTION;
import com.aionengine.gameserver.network.aion.serverpackets.SM_SYSTEM_MESSAGE;
import com.aionengine.gameserver.utils.PacketSendUtility;

/**
 * @author ATracer
 */
public class EmoteManager {

    /**
     * Npc starts attacking from idle state
     *
     * @param owner
     */
    public static final void emoteStartAttacking(Npc owner) {
        Creature target = (Creature) owner.getTarget();
        owner.unsetState(CreatureState.WALKING);
        if (!owner.isInState(CreatureState.WEAPON_EQUIPPED)) {
            owner.setState(CreatureState.WEAPON_EQUIPPED);
            PacketSendUtility
                    .broadcastPacket(owner, new SM_EMOTION(owner, EmotionType.START_EMOTE2, 0, target.getObjectId()));
            PacketSendUtility.broadcastPacket(owner, new SM_EMOTION(owner, EmotionType.ATTACKMODE, 0, target.getObjectId()));
        }
    }

    /**
     * Npc stops attacking
     *
     * @param owner
     */
    public static final void emoteStopAttacking(Npc owner) {
        owner.unsetState(CreatureState.WEAPON_EQUIPPED);
        if (owner.getTarget() != null && owner.getTarget() instanceof Player) {
            PacketSendUtility.sendPacket((Player) owner.getTarget(),
                    SM_SYSTEM_MESSAGE.STR_UI_COMBAT_NPC_RETURN(owner.getObjectTemplate().getNameId()));
        }
    }

    /**
     * Npc starts following other creature
     *
     * @param owner
     */
    public static final void emoteStartFollowing(Npc owner) {
        owner.unsetState(CreatureState.WALKING);
        PacketSendUtility.broadcastPacket(owner, new SM_EMOTION(owner, EmotionType.START_EMOTE2, 0, 0));
        PacketSendUtility.broadcastPacket(owner, new SM_EMOTION(owner, EmotionType.NEUTRALMODE, 0, 0));
    }

    /**
     * Npc starts walking (either random or path)
     *
     * @param owner
     */
    public static final void emoteStartWalking(Npc owner) {
        owner.setState(CreatureState.WALKING);
        PacketSendUtility.broadcastPacket(owner, new SM_EMOTION(owner, EmotionType.WALK));
    }

    /**
     * Npc stops walking
     *
     * @param owner
     */
    public static final void emoteStopWalking(Npc owner) {
        owner.unsetState(CreatureState.WALKING);
    }

    /**
     * Npc starts returning to spawn location
     *
     * @param owner
     */
    public static final void emoteStartReturning(Npc owner) {
        PacketSendUtility.broadcastPacket(owner, new SM_EMOTION(owner, EmotionType.START_EMOTE2, 0, 0));
        PacketSendUtility.broadcastPacket(owner, new SM_EMOTION(owner, EmotionType.NEUTRALMODE, 0, 0));
    }

    /**
     * Npc starts idling
     *
     * @param owner
     */
    public static final void emoteStartIdling(Npc owner) {
        owner.setState(CreatureState.WALKING);
        PacketSendUtility.broadcastPacket(owner, new SM_EMOTION(owner, EmotionType.START_EMOTE2, 0, 0));
        PacketSendUtility.broadcastPacket(owner, new SM_EMOTION(owner, EmotionType.NEUTRALMODE, 0, 0));
    }
}
